/**
 *  @Author YangYong  E-mail:yang2048@qq.com
 *  @Create 2017/9/28-21:32
 *
 */
layui.config({
    base: '/static/plugins/treeTable/js/'
}).use([ 'layer', 'table', 'element', 'form','jquery','extension'], function(){
    layer = parent.layer === undefined ? layui.layer : parent.layer
    ,table = layui.table //表格
    ,$ = layui.jquery
    ,element = layui.element
    ,form = layui.form
    ,extension = layui.extension;

    //验证
    form.verify({
        title: function (value, item) { //value：表单的值、item：表单的DOM对象
            if (value.length < 1) {
                return '角色名称不能为空';
            }
            if (!new RegExp("(^([\u0391-\uFFE5]|[a-zA-Z0-9]|[._-]){2,20}$)").test(value)) {
                return '用户名不合法(允许为2-20位汉字、字母、数字、"."、"-"、"_")';
            }
            if(/(^\_)|(\__)|(\_+$)/.test(value)){
                return '名称首尾不能出现下划线\'_\'';
            }
        }
    });
    // 监听表单提交
    form.on('submit(menuEditForm)', function (data) {
        $.ajax({
            async: false,
            type: "POST",
            url: "/menu/",
            data: data.field,
            dataType: 'json',
            success: function (res) {
                if (res.success) {
                    var index = layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                } else {
                    parent.layer.msg(res.statusMessage, {icon: 5, time: 2000});
                }
            },
            error: function (res) {
                parent.layer.msg(res.statusMessage, {icon: 5, time: 2000});
            }
        });
    });


    var active = {
        add: function(){ //获取选中数据
            layer.open({
                type: 2,
                title: '信息内容',
                shadeClose: true,
                shade: 0.3,
                maxmin: true,
                area: ['800px', '700px'],
                offset: '100px',
                content: ['/menu/toEdit', 'no']
            });
        }
        ,delBatch: function(){
            var checkStatus = table.checkStatus('demo')
                ,data = checkStatus.data;
            if (data.length == 0){
                layer.msg('选中了：'+ data.length + ' 个');
            }else {
                layer.alert(JSON.stringify(data));
            }
        }
    };

    $('.haerdTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    $('#tb').treegridData({
        id: 'id',
        parentColumn: 'parent',
        type: "GET", //请求数据的ajax类型
        url: '/menu/',   //请求数据的ajax的url
        ajaxParams: {}, //请求数据的ajax的data属性
        expandColumn: null,//在哪一列上面显示展开按钮
        striped: true,   //是否各行渐变色
        bordered: true,  //是否显示边框
        //expandAll: false,  //是否全部展开
        columns: [
            {
                title: '标题',
                field: 'title'
            },{
                title: '标识',
                field: 'code'
            },{
                title: '链接',
                field: 'href',
                formatter: function (value, row, index) {
                    return '<a href="' + value + '" >' + value + '</a>';
                }
            },{
                title: '排序',
                field: 'sort'
            },{
                title: '类型',
                field: 'type',
                formatter: function (value, row, index) {
                    if (value = '1') {
                        return '<span class="layui-btn layui-btn-mini">菜单</span>';
                    }
                }
            },{
                title: '状态',
                field: 'permissionStatus',
                formatter: function (value, row, index) { //value: 当前显示值 row:整个行的数据
                    if (value = '1'){
                        return '<button class="layui-btn layui-btn-mini layui-btn-radius layui-btn-normal">正 常</button>';
                    }
                    if (value = '0'){
                        return '<button class="layui-btn layui-btn-mini layui-btn-radius layui-btn-danger">隐藏</button>';
                    }
                }
            },{
                title: '操作',
                field: 'id',
                key:[{
                    'text':'编辑','type':'edit','class':'layui-btn layui-btn-mini'
                },{
                    'text':'删除','type':'del','class':'layui-btn layui-btn-mini layui-btn-danger'
                }]
            }
        ]
    });

});

function edit(id){
    layer.open({
        type: 2,
        title: '菜单信息',
        shadeClose: true,
        shade: 0.3,
        anim: 1,
        maxmin: true, //开启最大化最小化按钮
        area: ['1000px', '750px'],
        offset: '100px',
        content: '/menu/'+id,
    });
}
function del(id){
    layer.confirm('确认要删除吗？', function(index){
        $.ajax({
            async:false,
            type: "DELETE",
            url:"/menu/" + id,
            dataType: 'json',
            success: function(res){
                if(res.success){
                    layer.msg('成功', {
                        icon: 1,
                        time: 2000
                    }, function(){
                        parent.location.replace(location.href);
                    });
                }else {
                    parent.layer.msg(res.statusMessage, {icon: 5,time: 2000});
                }
            },
            error:function(res){
                parent.layer.msg(res.statusMessage, {icon: 5,time: 2000});
                return false;
            }
        });
        layer.close(index);
    });
}
